﻿@section header{

}
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-west">
        <input id="zTreesso" name="zTreesso" type="text" style="width:100%;height:25px;" placeholder="请输入关键字" />
        <ul id="itemTree" class="ztree"></ul>
    </div>
    <div class="ui-layout-center">
        <div class="topPanel">
            <div class="toolbar">
                <div class="btn-group">
                    <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
                </div>
                <div class="btn-group">
                    <a id="NF-add" authorize="yes" class="btn btn-primary" onclick="btn_add()"><i class="fa fa-plus"></i>新增</a>
                </div>
                <div class="btn-group">
                    <a id="NF-import" authorize="yes" class="btn btn-primary" onclick="btn_Imort()"><span class="glyphicon glyphicon-open"></span>导入</a>
                </div>
                <div class="btn-group">
                    <a id="NF-export" authorize="yes" class="btn btn-primary" onclick="btn_Export()"><i class="fa fa-file-excel-o"></i>导出</a>
                </div>
                <div class="btn-group">
                    <a id="NF-collect" authorize="yes" class="btn btn-primary" onclick="btn_Collect()"><i class="fa fa-file-excel-o"></i>收集</a>
                </div>
                <div class="operate">
                    <ul class="nav nav-pills">
                        <li class="first">已选中<span>1</span>项</li>
                        <li><a id="NF-edit" authorize="yes" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>修改菜单</a></li>
                        <li><a id="NF-delete" authorize="yes" onclick="btn_delete()"><i class="fa fa-trash-o"></i>删除菜单</a></li>
                        <li><a id="NF-Details" authorize="yes" onclick="btn_details()"><i class="fa fa-search-plus"></i>查看菜单</a></li>
                        <li class="split"></li>
                        <li><a id="NF-modulebutton" authorize="yes" onclick="btn_modulebutton()"><i class="fa fa-gavel"></i>按钮管理</a></li>
                    </ul>
                    <a href="javascript:;" class="close"></a>
                </div>
                <script>$('.toolbar').authorizeButton()</script>
            </div>
            <div class="search">
                <table>
                    <tr>
                        <td>
                            <div class="input-group">
                                <input id="txt_keyword" type="text" class="form-control" placeholder="请输入要查询关键字" style="width: 200px;">
                                <span class="input-group-btn">
                                    <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                                </span>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="gridPanel">
            <table id="gridList"></table>
            <div id="gridPager"></div>
        </div>
    </div>
</div>

@section scripts{
    <script>
        var baseUrl = "/rbac/Menu/";
        var treeNodeId = "";
        $(function () {
            $('#layout').layout();
            treeView();
            gridList();
        })
        function treeView() {
            var setting = {
                view: {
                    dblClickExpand: false,
                    showLine: true,
                    selectedMulti: false
                },
                data: {
                    key: {
                        name: "Name",
                    },
                    simpleData: {
                        enable: true,
                        idKey: "ID",
                        pIdKey: "ParentID",
                        rootPId: ""
                    }
                },
                callback: {
                    // 单击事件
                    onClick: function zTreeOnClick(event, treeId, treeNode) {
                        //console.log(treeNode)
                        treeNodeId = treeNode.ID;
                        $("#txt_keyword").val('');
                        $('#btn_search').trigger("click");
                    }
                }
            };
            $.ajax({
                url: baseUrl + "GetAll",
                type: "post",
                dataType: "json",
                success: function (data) {
                    var zTreeObj = $.fn.zTree.init($("#itemTree"), setting, data);
                    fuzzySearch('itemTree', '#zTreesso', null, true); //初始化模糊搜索方法
                }
            });
        }
        function gridList() {
            var $gridList = $("#gridList");
            $gridList.dataGrid({
                height: $(window).height() - 96,
                colModel: [
                    { label: "主键", name: "ID", hidden: true, key: true },
                    { label: '名称', name: 'Name', width: 200, align: 'left', frozen: true },
                    { label: '连接', name: 'Url', width: 260, align: 'left' },
                    { label: '图标', name: 'Icon', width: 100, align: 'left' },
                    {
                        label: "是否系统模块", name: "IsSys", width: 100, align: "center",
                        formatter: function (cellvalue, options, rowObject) {
                            var keyId = rowObject.ID;
                            return cellvalue == 0 ? "<i onclick=\"UpdateField('" + keyId + "','IsSys',0)\" class=\"fa fa-toggle-on\"></i>" :
                                "<i onclick=\"UpdateField('" + keyId + "','IsSys',1)\" class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    {
                        label: "有效", name: "Status", width: 100, align: "center",
                        formatter: function (cellvalue, options, rowObject) {
                            var keyId = rowObject.ID;
                            return cellvalue == 0 ? "<i onclick='Switch(\"" + keyId + "\")' class=\"fa fa-toggle-on\"></i>" : "<i onclick='Switch(\"" + keyId + "\")' class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    {
                        label: '排序', name: 'OrderBy', width: 100, align: 'left',
                        formatter: function (cellvalue, options, rowObject) {
                            var keyId = rowObject.ID;
                            return "<input type='text' name='OrderBy[" + keyId + "]' autocomplete='off' style='width: 50px; height: 20px;' onchange='update_field(\"" + keyId + "\",this.value)' value=" + cellvalue + " />";
                        }
                    }
                ],
                rowNum: 20,
                rowList: [20, 50, 100],
                pager: '#gridPager',
                sortname: 'OrderBy',
                viewrecords: false,
                sortorder: "asc"
            });
            $("#btn_search").click(function () {
                $gridList.jqGrid('setGridParam', {
                    url: baseUrl + "GetGridDataJson",
                    postData: { pId: treeNodeId, keyword: $("#txt_keyword").val() },
                }).trigger('reloadGrid');
            });
        }
        function btn_add() {
            $.modalOpen({
                id: "Form",
                title: "新增菜单",
                type: "POST",
                url: baseUrl + "Form",
                width: "700px",
                height: "440px",
                callBack: function (iframeId) {
                    top.frames[iframeId].submitForm();
                }
            });
        }
        function btn_edit() {
            var keyValue = $("#gridList").jqGridRowValue().ID;
            var title = $("#gridList").jqGridRowValue().Name;
            $.modalOpen({
                id: "Form",
                title: "修改【" + title + "】",
                type: "POST",
                url: baseUrl + "Form?keyValue=" + keyValue,
                width: "700px",
                height: "440px",
                callBack: function (iframeId) {
                    top.frames[iframeId].submitForm();
                }
            });
        }
        function btn_delete() {
            $.deleteForm({
                url: baseUrl + "DeleteForm",
                param: { keyValue: $("#gridList").jqGridRowValue().ID },
                success: function () {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            })
        }
        function btn_details() {
            var keyValue = $("#gridList").jqGridRowValue().ID;
            $.modalOpen({
                id: "Details",
                title: "查看菜单",
                url: baseUrl + "Details?keyValue=" + keyValue,
                width: "700px",
                height: "490px",
                btn: null,
            });
        }
        function btn_Export() {
            $("#gridList").jqGrid("excelExport", {
                includeLabels: true,
                includeGroupHeader: true,
                includeFooter: true,
                fileName: "MenuExport.xlsx",
                onBeforeExport: function (xlsx) {
                    var sheet = xlsx.xl.worksheets['sheet1.xml'];
                    $('row c[r^="D"]', sheet).each(function () {
                        if ($(this).text() * 1 >= 50) {
                            $(this).attr('s', '20');
                        }
                    });
                },
                maxlength: 40
            });
        }
        function btn_modulebutton() {
            var keyValue = $("#gridList").jqGridRowValue().ID;
            var title = $("#gridList").jqGridRowValue().Name;
            $.modalOpen({
                id: "modulebutton",
                title: "【" + title + "】按钮管理",
                url: "/rbac/MenuElement/Index?moduleId=" + keyValue,
                width: "950px",
                height: "600px",
                btn: null,
            });
        }
        function btn_Collect() {
            $.deleteForm({
                url: baseUrl + "Collect",
                prompt: "注：您确定要收集菜单页面命令数据吗？",
                loading: "正在收集数据...",
                success: function () {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            })
        }
        //关闭有效触发
        function Switch(keyId) {
            $.ajax({
                url: baseUrl + "Switch?keyId=" + keyId,
                data: {},
                type: "POST",
                dataType: "json",
                async: false,
                success: function () {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            });
        }
        function update_field(keyId, cellvalue) {
            $.ajax({
                url: baseUrl + "UpdateOrderBy?keyId=" + keyId + "&value=" + cellvalue,
                data: {},
                dataType: "json",
                type: "POST",
                success: function () {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            });
        }
        function UpdateField(keyId, fieldName, fieldValue) {
            $.ajax({
                url: baseUrl + "UpdateField",
                data: { keyId: keyId, fieldName: fieldName, fieldValue: fieldValue },
                type: "POST",
                dataType: "json",
                async: false,
                success: function () {
                    $.currentWindow().$("#gridList").resetSelection();
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            });
        }
    </script>
}
